home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / ArchiveUtils / Unarj / Source / unarj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  24.0 KB  |  1,041 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 12/03/91
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   I wish to give credit to Haruhiko Okumura for providing the
  18.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  19.  *   that UNARJ is significantly different from AR from an archive
  20.  *   structural point of view.
  21.  *
  22.  * Modification history:
  23.  * Date      Programmer  Description of modification.
  24.  * 04/05/91  R. Jung     Rewrote code.
  25.  * 04/23/91  M. Adler    Portabilized.
  26.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  27.  *                       fillbuf().
  28.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  29.  * 05/25/91  R. Jung     Improved find_header().
  30.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  31.  *                       set_ftime_mode().
  32.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  33.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  34.  *                       Added strlower().
  35.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  36.  * 07/21/91  R. Jung     Added #ifdef VMS.
  37.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  38.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  39.  *                       per Eric Larson.
  40.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  41.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  42.  * 11/24/91  R. Jung     Added more error_count processing.
  43.  * 12/03/91  R. Jung     Added backup file processing.
  44.  *
  45.  */
  46.  
  47. #include "unarj.h"
  48.  
  49. #ifdef MODERN
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <ctype.h>
  53. #else /* !MODERN */
  54. extern void free();
  55. extern void exit();
  56. extern char *strcat();
  57. extern char *strcpy();
  58. extern char *strncpy();
  59. extern char *strchr();
  60. extern char *strrchr();
  61. extern int strlen();
  62. extern int strcmp();
  63. #ifdef VMS
  64. #include <ssdef.h>
  65. #define EXIT_FAILURE SS$_ABORT
  66. #define EXIT_SUCCESS SS$_NORMAL
  67. #else
  68. #define EXIT_FAILURE (1)
  69. #define EXIT_SUCCESS (0)
  70. #endif
  71. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  72. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  73. #endif /* ?MODERN */
  74.  
  75. #ifdef THINK_C
  76. #include <console.h>
  77. #endif
  78.  
  79. /* Global variables */
  80.  
  81. UCRC   crc;
  82. FILE   *arcfile;
  83. FILE   *outfile;
  84. ushort bitbuf;
  85. long   compsize;
  86. long   origsize;
  87. uchar  subbitbuf;
  88. uchar  header[HEADERSIZE_MAX];
  89. char   arc_name[FNAME_MAX];
  90. int    command;
  91. int    bitcount;
  92. int    file_type;
  93. int    no_output;
  94. int    error_count;
  95.  
  96. /* Messages */
  97.  
  98. static char *M_USAGE  [] =
  99. {
  100. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  101. "        UNARJ e archive        (extract archive)\n",
  102. "        UNARJ l archive        (list archive)\n",
  103. "        UNARJ t archive        (test archive)\n",
  104. "        UNARJ x archive        (extract with pathnames)\n",
  105. "\n",
  106. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  107. "You may freely use, copy and distribute this program, provided that no fee\n",
  108. "is charged for such use, copying or distribution, and it is distributed\n",
  109. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  110. "warranty of any kind, express or implied, including but not limited to\n",
  111. "the implied warranties of merchantability and fitness for a particular\n",
  112. "purpose.  Refer to UNARJ.DOC for more warranty information.  If you find\n",
  113. "UNARJ of value, a gift of $10 or any amount would greatly appreciated.\n",
  114. "\n",
  115. "Robert K Jung                   Internet address :  robjung@world.std.com\n",
  116. "2606 Village Road West          CompuServe userid:  72077,445\n",
  117. "Norwood, Massachusetts 02062\n",
  118. "USA\n",
  119. #ifdef _NeXT    /* _NeXT */
  120. "\n",
  121. " NOTE: This copy of UNARJ was modified for the NeXT by Jason Fosback.\n",
  122. " Please see the documentation included with the source code for details.\n",
  123. #endif /* _NeXT */
  124. NULL
  125. };
  126.  
  127. char M_VERSION [] = "UNARJ (Demo version) 2.30 Copyright (c) 1991 Robert K Jung\n\n";
  128.  
  129. char M_ARCDATE [] = "Archive date      : %s\n";
  130. char M_BADCOMND[] = "Bad UNARJ command: %s";
  131. char M_BADCOMNT[] = "Invalid comment header";
  132. char M_BADHEADR[] = "Bad header";
  133. char M_BADTABLE[] = "Bad Huffman code";
  134. char M_CANTOPEN[] = "Can't open %s";
  135. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  136. char M_CANTWRIT[] = "Can't write file. Disk full?";
  137. char M_CRCERROR[] = "CRC error!\n";
  138. char M_CRCOK   [] = "CRC OK\n";
  139. char M_DIFFHOST[] = "  Binary file!";
  140. char M_ENCRYPT [] = "File is password encrypted, ";
  141. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  142. char M_EXTRACT [] = "Extracting %-25s";
  143. char M_FEXISTS [] = "%-25s exists, ";
  144. char M_HEADRCRC[] = "Header CRC error!";
  145. char M_NBRFILES[] = "%5d file(s)\n";
  146. char M_NOMEMORY[] = "Out of memory";
  147. char M_NOTARJ  [] = "%s is not an ARJ archive";
  148. char M_PROCARC [] = "Processing archive: %s\n";
  149. char M_SKIPPED [] = "Skipped %s\n";
  150. char M_SUFFIX  [] = ARJ_SUFFIX;
  151. char M_TESTING [] = "Testing    %-25s";
  152. char M_UNKNMETH[] = "Unsupported method: %d, ";
  153. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  154. char M_UNKNVERS[] = "Unsupported version: %d, ";
  155. #ifdef _NeXT    /* _NeXT */
  156. char M_CANTDIRS[] = "Couldn't create directory for %s";
  157. #endif /* _NeXT */
  158.  
  159. #define get_crc()       get_longword()
  160. #define fget_crc(f)     fget_longword(f)
  161.  
  162. #define setup_get(PTR)  (get_ptr = (PTR))
  163. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  164.  
  165. #define BUFFERSIZE      4096
  166.  
  167. #define ASCII_MASK      0x7F
  168.  
  169. #define CRCPOLY         0xEDB88320L
  170.  
  171. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  172.  
  173. /* Local functions */
  174.  
  175. #ifdef MODERN
  176. static void  make_crctable(void);
  177. static void  crc_buf(char *str, int len);
  178. static void  strparity(uchar *p);
  179. static FILE  *fopen_msg(char *name, char *mode);
  180. static int   fget_byte(FILE *f);
  181. static uint  fget_word(FILE *f);
  182. static ulong fget_longword(FILE *f);
  183. static void  fread_crc(uchar *p, int n, FILE *f);
  184. static void  decode_path(char *name);
  185. static void  get_date_str(char *str, ulong tstamp);
  186. static int   parse_path(char *pathname, char *path, char *entry);
  187. static void  strncopy(char *to, char *from, int len);
  188. static uint  get_word(void);
  189. static ulong get_longword(void);
  190. static long  find_header(FILE *fd);
  191. static int   read_header(int first, FILE *fd, char *name);
  192. static void  skip(void);
  193. static void  unstore(void);
  194. static int   check_flags(void);
  195. static int   extract(void);
  196. static int   test(void);
  197. static uint  ratio(long a, long b);
  198. static void  list_start(void);
  199. static void  list_arc(int count);
  200. static void  execute_cmd(void);
  201. static void  help(void);
  202. #endif /* MODERN */
  203.  
  204. /* Local variables */
  205.  
  206. static char   filename[FNAME_MAX];
  207. static char   comment[COMMENT_MAX];
  208. static char   *hdr_filename;
  209. static char   *hdr_comment;
  210.  
  211. static ushort headersize;
  212. static uchar  first_hdr_size;
  213. static uchar  arj_nbr;
  214. static uchar  arj_x_nbr;
  215. static uchar  host_os;
  216. static uchar  arj_flags;
  217. static short  method;
  218. static uint   file_mode;
  219. static ulong  time_stamp;
  220. static short  entry_pos;
  221. static ushort host_data;
  222. static uchar  *get_ptr;
  223. static UCRC   file_crc;
  224. static UCRC   header_crc;
  225.  
  226. static long   first_hdr_pos;
  227. static long   torigsize;
  228. static long   tcompsize;
  229.  
  230. static int    clock_inx;
  231.  
  232. static char   *writemode[2]  = { "wb",  "w" };
  233.  
  234. static UCRC   crctable[UCHAR_MAX + 1];
  235.  
  236. /* Functions */
  237.  
  238. static void
  239. make_crctable()
  240. {
  241.     uint i, j;
  242.     UCRC r;
  243.  
  244.     for (i = 0; i <= UCHAR_MAX; i++)
  245.     {
  246.         r = i;
  247.         for (j = CHAR_BIT; j > 0; j--)
  248.         {
  249.             if (r & 1)
  250.                 r = (r >> 1) ^ CRCPOLY;
  251.             else
  252.                 r >>= 1;
  253.         }
  254.         crctable[i] = r;
  255.     }
  256. }
  257.  
  258. static void
  259. crc_buf(str, len)
  260. char *str;
  261. int  len;
  262. {
  263.     while (len--)
  264.         UPDATE_CRC(crc, *str++);
  265. }
  266.  
  267. void
  268. disp_clock()
  269. {
  270.     static char clock_str[4] = { '|', '/', '-', '\\' };
  271.  
  272.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  273.     clock_inx = (clock_inx + 1) & 0x03;
  274. }
  275.  
  276. void
  277. error(fmt, arg)
  278. char *fmt;
  279. char *arg;
  280. {
  281.     putc('\n', stdout);
  282.     printf(fmt, arg, error_count);
  283.     putc('\n', stdout);
  284.     exit(EXIT_FAILURE);
  285. }
  286.  
  287. static void
  288. strparity(p)
  289. uchar *p;
  290. {
  291.     while (*p)
  292.     {
  293.         FIX_PARITY(*p);
  294.         p++;
  295.     }
  296. }
  297.  
  298. static FILE *
  299. fopen_msg(name, mode)
  300. char *name;
  301. char *mode;
  302. {
  303.     FILE *fd;
  304.  
  305.     fd = file_open(name, mode);
  306.     if (fd == NULL)
  307.         error(M_CANTOPEN, name);
  308.     return fd;
  309. }
  310.  
  311. static int
  312. fget_byte(f)
  313. FILE *f;
  314. {
  315.     int c;
  316.  
  317.     if ((c = getc(f)) == EOF)
  318.         error(M_CANTREAD, "");
  319.     return c & 0xFF;
  320. }
  321.  
  322. static uint
  323. fget_word(f)
  324. FILE *f;
  325. {
  326.     uint b0, b1;
  327.  
  328.     b0 = fget_byte(f);
  329.     b1 = fget_byte(f);
  330.     return (b1 << 8) + b0;
  331. }
  332.  
  333. static ulong
  334. fget_longword(f)
  335. FILE *f;
  336. {
  337.     ulong b0, b1, b2, b3;
  338.  
  339.     b0 = fget_byte(f);
  340.     b1 = fget_byte(f);
  341.     b2 = fget_byte(f);
  342.     b3 = fget_byte(f);
  343.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  344. }
  345.  
  346. static void
  347. fread_crc(p, n, f)
  348. uchar *p;
  349. int   n;
  350. FILE  *f;
  351. {
  352.     n = file_read((char *)p, 1, n, f);
  353.     origsize += n;
  354.     crc_buf((char *)p, n);
  355. }
  356.  
  357. void
  358. fwrite_txt_crc(p, n)
  359. uchar *p;
  360. int   n;
  361. {
  362.     uchar c;
  363.  
  364.     crc_buf((char *)p, n);
  365.     if (no_output)
  366.         return;
  367.  
  368.     if (file_type == TEXT_TYPE)
  369.     {
  370.         while (n--)
  371.         {
  372.             c = *p++;
  373.             if (host_os != OS)
  374.             {
  375.                 FIX_PARITY(c);
  376.             }
  377.             if (putc((int) c, outfile) == EOF)
  378.                 error(M_CANTWRIT, "");
  379.         }
  380.     }
  381.     else
  382.     {
  383.         if (file_write((char *)p, 1, n, outfile) != n)
  384.             error(M_CANTWRIT, "");
  385.     }
  386. }
  387.  
  388. void
  389. init_getbits()
  390. {
  391.     bitbuf = 0;
  392.     subbitbuf = 0;
  393.     bitcount = 0;
  394.     fillbuf(2 * CHAR_BIT);
  395. }
  396.  
  397. void
  398. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  399. int n;
  400. {
  401.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  402.     while (n > bitcount)
  403.     {
  404.         bitbuf |= subbitbuf << (n -= bitcount);
  405.         if (compsize != 0)
  406.         {
  407.             compsize--;
  408.             subbitbuf = (uchar) getc(arcfile);
  409.         }
  410.         else
  411.             subbitbuf = 0;
  412.         bitcount = CHAR_BIT;
  413.     }
  414.     bitbuf |= subbitbuf >> (bitcount -= n);
  415. }
  416.  
  417. ushort
  418. getbits(n)
  419. int n;
  420. {
  421.     ushort x;
  422.  
  423.     x = bitbuf >> (2 * CHAR_BIT - n);
  424.     fillbuf(n);
  425.     return x;
  426. }
  427.  
  428. static void
  429. decode_path(name)
  430. char *name;
  431. {
  432.     for ( ; *name; name++)
  433.     {
  434.         if (*name == ARJ_PATH_CHAR)
  435.             *name = PATH_CHAR;
  436.     }
  437. }
  438.  
  439. static void
  440. get_date_str(str, tstamp)
  441. char  *str;
  442. ulong tstamp;
  443. {
  444.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  445.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  446.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  447. }
  448.  
  449. static int
  450. parse_path(pathname, path, entry)
  451. char *pathname;
  452. char *path;
  453. char *entry;
  454. {
  455.     char *cptr, *ptr, *fptr;
  456.     short pos;
  457.  
  458.     fptr = NULL;
  459.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  460.     {
  461.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  462.                 (fptr == NULL || ptr > fptr))
  463.             fptr = ptr;
  464.     }
  465.     if (fptr == NULL)
  466.         pos = 0;
  467.     else
  468.         pos = fptr + 1 - pathname;
  469.     if (path != NULL)
  470.     {
  471.        strncpy(path, pathname, pos);
  472.        path[pos] = NULL_CHAR;
  473.     }
  474.     if (entry != NULL)
  475.        strcpy(entry, &pathname[pos]);
  476.     return pos;
  477. }
  478.  
  479. static void
  480. strncopy(to, from, len)
  481. char *to;
  482. char *from;
  483. int  len;
  484. {
  485.     int i;
  486.  
  487.     for (i = 1; i < len && *from; i++)
  488.         *to++ = *from++;
  489.     *to = NULL_CHAR;
  490. }
  491.  
  492. void
  493. strlower(s)
  494. char *s;
  495. {
  496.     while (*s)
  497.     {
  498.         *s = (char) tolower(*s);
  499.         s++;
  500.     }
  501. }
  502.  
  503. void
  504. strupper(s)
  505. char *s;
  506. {
  507.     while (*s)
  508.     {
  509.         *s = (char) toupper(*s);
  510.         s++;
  511.     }
  512. }
  513.  
  514. voidp *
  515. malloc_msg(size)
  516. int size;
  517. {
  518.     char *p;
  519.  
  520.     if ((p = (char *)xmalloc(size)) == NULL)
  521.         error(M_NOMEMORY, "");
  522.     return (voidp *)p;
  523. }
  524.  
  525. static uint
  526. get_word()
  527. {
  528.     uint b0, b1;
  529.  
  530.     b0 = get_byte();
  531.     b1 = get_byte();
  532.     return (b1 << 8) + b0;
  533. }
  534.  
  535. static ulong
  536. get_longword()
  537. {
  538.     ulong b0, b1, b2, b3;
  539.  
  540.     b0 = get_byte();
  541.     b1 = get_byte();
  542.     b2 = get_byte();
  543.     b3 = get_byte();
  544.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  545. }
  546.  
  547. static long
  548. find_header(fd)
  549. FILE *fd;
  550. {
  551.     long arcpos, lastpos;
  552.     int c;
  553.  
  554.     arcpos = file_tell(fd);
  555.     file_seek(fd, 0L, SEEK_END);
  556.     lastpos = file_tell(fd) - 2;
  557.     if (lastpos > MAXSFX)
  558.         lastpos = MAXSFX;
  559.     for ( ; arcpos < lastpos; arcpos++)
  560.     {
  561.         file_seek(fd, arcpos, SEEK_SET);
  562.         c = fget_byte(fd);
  563.         while (arcpos < lastpos)
  564.         {
  565.             if (c != HEADER_ID_LO)  /* low order first */
  566.                 c = fget_byte(fd);
  567.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  568.                 break;
  569.             arcpos++;
  570.         }
  571.         if (arcpos >= lastpos)
  572.             break;
  573.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  574.         {
  575.             crc = CRC_MASK;
  576.             fread_crc(header, (int) headersize, fd);
  577.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  578.             {
  579.                 file_seek(fd, arcpos, SEEK_SET);
  580.                 return arcpos;
  581.             }
  582.         }
  583.     }
  584.     return -1;          /* could not find a valid header */
  585. }
  586.  
  587. static int
  588. read_header(first, fd, name)
  589. int  first;
  590. FILE *fd;
  591. char *name;
  592. {
  593.     ushort extheadersize, header_id;
  594.  
  595.     header_id = fget_word(fd);
  596.     if (header_id != HEADER_ID)
  597.     {
  598.         if (first)
  599.             error(M_NOTARJ, name);
  600.         else
  601.             error(M_BADHEADR, "");
  602.     }
  603.  
  604.     headersize = fget_word(fd);
  605.     if (headersize == 0)
  606.         return 0;               /* end of archive */
  607.     if (headersize > HEADERSIZE_MAX)
  608.         error(M_BADHEADR, "");
  609.  
  610.     crc = CRC_MASK;
  611.     fread_crc(header, (int) headersize, fd);
  612.     header_crc = fget_crc(fd);
  613.     if ((crc ^ CRC_MASK) != header_crc)
  614.         error(M_HEADRCRC, "");
  615.  
  616.     setup_get(header);
  617.     first_hdr_size = get_byte();
  618.     arj_nbr = get_byte();
  619.     arj_x_nbr = get_byte();
  620.     host_os = get_byte();
  621.     arj_flags = get_byte();
  622.     method = get_byte();
  623.     file_type = get_byte();
  624.     (void)get_byte();
  625.     time_stamp = get_longword();
  626.     compsize = get_longword();
  627.     origsize = get_longword();
  628.     file_crc = get_crc();
  629.     entry_pos = get_word();
  630.     file_mode = get_word();
  631.     host_data = get_word();
  632.  
  633.     hdr_filename = (char *)&header[first_hdr_size];
  634.     strncopy(filename, hdr_filename, sizeof(filename));
  635.     if (host_os != OS)
  636.         strparity((uchar *)filename);
  637.     if ((arj_flags & PATHSYM_FLAG) != 0)
  638.         decode_path(filename);
  639.  
  640.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  641.     strncopy(comment, hdr_comment, sizeof(comment));
  642.     if (host_os != OS)
  643.         strparity((uchar *)comment);
  644.  
  645.     /* if extheadersize == 0 then no CRC */
  646.     /* otherwise read extheader data and read 4 bytes for CRC */
  647.  
  648.     while ((extheadersize = fget_word(fd)) != 0)
  649.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  650.  
  651.     return 1;                   /* success */
  652. }
  653.  
  654. static void
  655. skip()
  656. {
  657.     file_seek(arcfile, compsize, SEEK_CUR);
  658. }
  659.  
  660. static void
  661. unstore()
  662. {
  663.     int n;
  664.     long pos;
  665.     char *buffer;
  666.  
  667.     buffer = (char *)malloc_msg(BUFFERSIZE);
  668.     pos = file_tell(arcfile);
  669.     disp_clock();
  670.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  671.     n = compsize > (long)n ? n : (int)compsize;
  672.     while (compsize > 0)
  673.     {
  674.         if (file_read(buffer, 1, n, arcfile) != n)
  675.             error(M_CANTREAD, "");
  676.         disp_clock();
  677.         compsize -= n;
  678.         fwrite_txt_crc((uchar *)buffer, n);
  679.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  680.     }
  681.     free(buffer);
  682. }
  683.  
  684. static int
  685. check_flags()
  686. {
  687.     if (arj_x_nbr > ARJ_X_VERSION)
  688.     {
  689.         printf(M_UNKNVERS, arj_x_nbr);
  690.         printf(M_SKIPPED, filename);
  691.         skip();
  692.         return -1;
  693.     }
  694.     if ((arj_flags & GARBLE_FLAG) != 0)
  695.     {
  696.         printf(M_ENCRYPT);
  697.         printf(M_SKIPPED, filename);
  698.         skip();
  699.         return -1;
  700.     }
  701.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  702.     {
  703.         printf(M_UNKNMETH, method);
  704.         printf(M_SKIPPED, filename);
  705.         skip();
  706.         return -1;
  707.     }
  708.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  709.     {
  710.         printf(M_UNKNTYPE, file_type);
  711.         printf(M_SKIPPED, filename);
  712.         skip();
  713.         return -1;
  714.     }
  715.     return 0;
  716. }
  717.  
  718. static int
  719. extract()
  720. {
  721.     char name[FNAME_MAX];
  722.  
  723.     if (check_flags())
  724.     {
  725.         error_count++;
  726.         return 0;
  727.     }
  728.  
  729.     no_output = 0;
  730.     if (command == 'E')
  731.         strcpy(name, &filename[entry_pos]);
  732.     else
  733.     {
  734.         strcpy(name, DEFAULT_DIR);
  735.         strcat(name, filename);
  736.     }
  737.  
  738.     if (host_os != OS)
  739. {
  740.  
  741. #ifdef _NeXT    /* _NeXT */
  742.  
  743.             /* This is to check if the .arj file  */
  744.             /* was made on a Unix machine.  If so,*/
  745.             /* we'll just not change the case.    */
  746.         if (host_os != 2) {
  747.             default_case_path(name);
  748.             }
  749. #else /* non-NeXT */
  750.         default_case_path(name);
  751. #endif /* _NeXT */
  752.         }
  753.     if (file_exists(name))
  754.     {
  755.         printf(M_FEXISTS, name);
  756.         printf(M_SKIPPED, name);
  757.         skip();
  758.         error_count++;
  759.         return 0;
  760.     }
  761.  
  762. #ifdef _NeXT    /* _NeXT */
  763.  
  764.     if (create_directories(name)) {
  765.         printf(M_CANTDIRS, name);
  766.         putchar('\n');
  767.         skip();
  768.         error_count++;
  769.         return 0;
  770.         }
  771.  
  772. #endif /* _NeXT */
  773.  
  774.     outfile = file_open(name, writemode[file_type & 1]);
  775.     if (outfile == NULL)
  776.     {
  777.         printf(M_CANTOPEN, name);
  778.         putchar('\n');
  779.         skip();
  780.         error_count++;
  781.         return 0;
  782.     }
  783.     printf(M_EXTRACT, name);
  784.     if (host_os != OS && file_type == BINARY_TYPE)
  785.         printf(M_DIFFHOST);
  786.     printf("  ");
  787.  
  788.     crc = CRC_MASK;
  789.  
  790.     if (method == 0)
  791.         unstore();
  792.     else if (method == 1 || method == 2 || method == 3)
  793.         decode();
  794.     else if (method == 4)
  795.         decode_f();
  796.     fclose(outfile);
  797.  
  798.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  799.  
  800.     if ((crc ^ CRC_MASK) == file_crc)
  801.         printf(M_CRCOK);
  802.     else
  803.     {
  804.         printf(M_CRCERROR);
  805.         error_count++;
  806.     }
  807.     return 1;
  808. }
  809.  
  810. static int
  811. test()
  812. {
  813.     if (check_flags())
  814.         return 0;
  815.  
  816.     no_output = 1;
  817.     printf(M_TESTING, filename);
  818.     printf("  ");
  819.  
  820.     crc = CRC_MASK;
  821.  
  822.     if (method == 0)
  823.         unstore();
  824.     else if (method == 1 || method == 2 || method == 3)
  825.         decode();
  826.     else if (method == 4)
  827.         decode_f();
  828.  
  829.     if ((crc ^ CRC_MASK) == file_crc)
  830.         printf(M_CRCOK);
  831.     else
  832.     {
  833.         printf(M_CRCERROR);
  834.         error_count++;
  835.     }
  836.     return 1;
  837. }
  838.  
  839. static uint
  840. ratio(a, b)
  841. long a, b;
  842. {
  843.    int i;
  844.  
  845.    for (i = 0; i < 3; i++)
  846.        if (a <= LONG_MAX / 10)
  847.            a *= 10;
  848.        else
  849.            b /= 10;
  850.    if ((long) (a + (b >> 1)) < a)
  851.    {
  852.        a >>= 1;
  853.        b >>= 1;
  854.    }
  855.    if (b == 0)
  856.        return 0;
  857.    return (uint) ((a + (b >> 1)) / b);
  858. }
  859.  
  860. static void
  861. list_start()
  862. {
  863.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  864.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  865. }
  866.  
  867. static void
  868. list_arc(count)
  869. int count;
  870. {
  871.     uint r;
  872.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  873.     char date_str[20], fmode_str[10];
  874.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  875.     static char pthf[2] = { ' ', '+' };
  876.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  877.     static char volf[2] = { ' ', 'V' };
  878.     static char extf[2] = { ' ', 'X' };
  879.     static char bckf[2] = { ' ', '*' };
  880.  
  881.     if (count == 0)
  882.         list_start();
  883.  
  884.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  885.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  886.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  887.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  888.     path_mode   = (entry_pos > 0);
  889.     r = ratio(compsize, origsize);
  890.     torigsize += origsize;
  891.     tcompsize += compsize;
  892.     ftype = file_type;
  893.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  894.             ftype != LABEL_TYPE)
  895.         ftype = 3;
  896.     get_date_str(date_str, time_stamp);
  897.     strcpy(fmode_str, "    ");
  898.     if (host_os == OS)
  899.         get_mode_str(fmode_str, (uint) file_mode);
  900.     if (strlen(&filename[entry_pos]) > 12)
  901.         printf("%-12s\n             ", &filename[entry_pos]);
  902.     else
  903.         printf("%-12s ", &filename[entry_pos]);
  904.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  905.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  906.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  907.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  908. }
  909.  
  910. static void
  911. execute_cmd()
  912. {
  913.     int file_count;
  914.     char date_str[22];
  915.     uint r;
  916.  
  917.     first_hdr_pos = 0;
  918.     time_stamp = 0;
  919.     first_hdr_size = FIRST_HDR_SIZE;
  920.  
  921.     arcfile = fopen_msg(arc_name, "rb");
  922.  
  923.     printf(M_PROCARC, arc_name);
  924.  
  925.     first_hdr_pos = find_header(arcfile);
  926.     if (first_hdr_pos < 0)
  927.         error(M_NOTARJ, arc_name);
  928.     file_seek(arcfile, first_hdr_pos, SEEK_SET);
  929.     if (!read_header(1, arcfile, arc_name))
  930.         error(M_BADCOMNT, "");
  931.     get_date_str(date_str, time_stamp);
  932.     printf(M_ARCDATE, date_str);
  933.  
  934.     file_count = 0;
  935.     while (read_header(0, arcfile, arc_name))
  936.     {
  937.         switch (command)
  938.         {
  939.         case 'E':
  940.         case 'X':
  941.             if (extract())
  942.                 file_count++;
  943.             break;
  944.         case 'L':
  945.             list_arc(file_count++);
  946.             skip();
  947.             break;
  948.         case 'T':
  949.             if (test())
  950.                 file_count++;
  951.             break;
  952.         }
  953.     }
  954.  
  955.     if (command == 'L')
  956.     {
  957.         printf("------------ ---------- ---------- ----- -----------------\n");
  958.         r = ratio(tcompsize, torigsize);
  959.         printf(" %5d files %10ld %10ld %u.%03u %s\n",
  960.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  961.     }
  962.     else
  963.         printf(M_NBRFILES, file_count);
  964.  
  965.     fclose(arcfile);
  966. }
  967.  
  968. static void
  969. help()
  970. {
  971.     int i;
  972.  
  973.     for (i = 0; M_USAGE[i] != NULL; i++)
  974.         printf(M_USAGE[i]);
  975. }
  976.  
  977. int
  978. main(argc, argv)
  979. int  argc;
  980. char *argv[];
  981. {
  982.     int i, j, lastc;
  983.     char *arc_p;
  984.  
  985. #ifdef THINK_C
  986.     argc = ccommand(&argv);
  987. #endif
  988.  
  989.     printf(M_VERSION);
  990.  
  991.     if (argc == 1)
  992.     {
  993.         help();
  994.         return EXIT_SUCCESS;
  995.     }
  996.     else if (argc == 2)
  997.     {
  998.         command = 'L';
  999.         arc_p = argv[1];
  1000.     }
  1001.     else if (argc == 3)
  1002.     {
  1003.         if (strlen(argv[1]) > 1)
  1004.             error(M_BADCOMND, argv[1]);
  1005.         command = toupper(*argv[1]);
  1006.         if (strchr("ELTX", command) == NULL)
  1007.             error(M_BADCOMND, argv[1]);
  1008.         arc_p = argv[2];
  1009.     }
  1010.     else
  1011.     {
  1012.         help();
  1013.         return EXIT_FAILURE;
  1014.     }
  1015.  
  1016.     strncopy(arc_name, arc_p, FNAME_MAX);
  1017.     case_path(arc_name);
  1018.     i = strlen(arc_name);
  1019.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  1020.     lastc = arc_name[i - 1];
  1021.     if (lastc == ARJ_DOT)
  1022.         arc_name[i - 1] = NULL_CHAR;
  1023.     else if (strchr(&arc_name[j], ARJ_DOT) == NULL)
  1024.         strcat(arc_name, M_SUFFIX);
  1025.  
  1026.     make_crctable();
  1027.  
  1028.     error_count = 0;
  1029.     clock_inx = 0;
  1030.     arcfile = NULL;
  1031.     outfile = NULL;
  1032.  
  1033.     execute_cmd();
  1034.  
  1035.     if (error_count > 0)
  1036.         error(M_ERRORCNT, "");
  1037.  
  1038.     return EXIT_SUCCESS;
  1039. }
  1040.  
  1041. /* end UNARJ.C */
  1042.